package UI.Main;

import BLL.FileSearch;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 搜索界面
 */
public class SearchPage extends JPanel implements ActionListener
{
    public static SearchPage _instance;
    JPanel searchPanel;
    int searchDir = -1;
    int searchFile = -1;
    JButton file,dir,search;
    JTextField searchText;
    JScrollPane scrollShow;
    public static JList<String> list;
    public DefaultListModel defaultListModel = new DefaultListModel();
    public int iconCounter = 0;
    public Icon[] icons = new Icon[999999];
    public static Map<String, String> mapSearch = new HashMap<>();
    String saimFile = "",sCur_URL = "",sText = "";
    boolean flagDir =false, flagFile =false;
    JPopupMenu sjPopupMenu;
    JMenuItem[] sJMIs = new JMenuItem[10];

    /**
     * 搜索界面构造函数
     */
    public SearchPage()
    {
        list = new JList<>();
        this._instance=this;
        this.setLayout(null);
        searchPanel=new JPanel();
        searchPanel.setBounds(0,0,1210,50);
        searchPanel.setBackground(Color.gray);
        searchPanel.setLayout(null);
        file=new JButton("文件");
        file.setFont(new Font("宋体", 1, 28));
        file.setBounds(700, 0, 100, 50);
        file.addActionListener(this);
        file.setBackground(Color.gray);
        file.setForeground(Color.white);
        searchPanel.add(file);
        
        dir=new JButton("目录");
        dir.setFont(new Font("宋体", 1, 28));
        dir.setBounds(800, 0, 100, 50);
        dir.addActionListener(this);
        dir.setBackground(Color.gray);
        dir.setForeground(Color.white);
        searchPanel.add(dir);
        
        search=new JButton("搜索");
        search.setFont(new Font("宋体", 1, 28));
        search.setBounds(950, 0, 100, 50);
        search.addActionListener(this);
        search.setBackground(Color.gray);
        search.setForeground(Color.white);
        searchPanel.add(search);
        
        this.add(searchPanel);
        searchText = new JTextField();
        searchText.addActionListener(this);
        searchText.setBounds(150,0,550,50);
        searchText.setFont(new  java.awt.Font("宋体",  1,  28));
        searchPanel.add(searchText);
        scrollShow = new JScrollPane(list);
        scrollShow.setBounds(0,50,1210,802);
        this.add(scrollShow);

        sjPopupMenu = new JPopupMenu();//文件/文件夹的属性菜单
        sJMIs[0] = new JMenuItem("打开");
        sJMIs[0].addActionListener(this);
        sjPopupMenu.add(sJMIs[0]);
        list.add(sjPopupMenu);

        list.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent e)
            {
                if(list.getSelectedIndex() != -1)
                {
                    if(e.getClickCount() == 1)
                    {   //单击
                        oneClick(list.getSelectedValue());
                    }
                    else if(e.getClickCount() == 2)
                    {   //双击打开文件/目录
                        System.out.println(list.getSelectedValue());
                        twoClick(list.getSelectedValue());
                    }
                    if(e.getButton() == 3 && list.getSelectedValuesList().size() == 1)
                    {   //右键菜单
                        sjPopupMenu.show(list,e.getX(),e.getY());//只支持单文件打开
                    }
                }
            }
        });
    }

    /**
     * 动作处理
     * @param e 动作事件
     */
    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == file)
        {   //文件
            searchFile *= -1;
            if(searchFile == -1)
            {
                file.setBackground(Color.gray);
                file.setForeground(Color.white);
            }
            else
            {
                file.setBackground(Color.white);
                file.setForeground(Color.gray);
            }
        }
        else if(e.getSource() == dir)
        {   //目录
            searchDir *= -1;
            if(searchDir == -1)
            {
                dir.setBackground(Color.gray);
                dir.setForeground(Color.white);
            }
            else
            {
                dir.setBackground(Color.white);
                dir.setForeground(Color.gray);
            }
        }
        else if(e.getSource() == searchText || e.getSource() == search)
        {   //搜索
            flagDir = false;
            flagFile = false;
            if(searchDir == 1)
                flagDir =true;
            if(searchFile == 1)
                flagFile =true;
            if((searchFile + searchDir) == -2)
            {
                JOptionPane.showMessageDialog(null, "请至少选择一个搜索类别", "提示", JOptionPane.YES_OPTION);
            }
            else
            {
                //MainForm.isSearching = true;
                defaultListModel.clear();
                iconCounter = 0;
                icons = new Icon[1000010];
                sCur_URL = MainForm.Cur_URL;
                sText = searchText.getText();
                FileSearch.bfsFile(sCur_URL, sText, flagDir, flagFile);
                list.setModel(defaultListModel);
                list.setCellRenderer(new UI.Main.MyCellRenderer(icons));
            }
        }
        else if(e.getSource() == sJMIs[0])
        {	//打开文件/文件夹
            File file = new File(mapSearch.get(list.getSelectedValue()));
            openFileorDict(file);
        }

    }

    /**
     * 单击
     * @param fileName 当前选择的文件名
     */
    private void oneClick(String fileName)
    {
        saimFile = sCur_URL + fileName;
    }

    /**
     * 双击
     * @param fileName 当前选择的文件名
     */
    public void twoClick(String fileName)
    {
        File file = new File(mapSearch.get(fileName));
        openFileorDict(file);
    }

    /**
     * 打开文件/目录
     * @param file 当前选择的文件
     */
    public static void openFileorDict(File file)
    {
        try
        {
            System.out.println(file);
            Desktop.getDesktop().open(file);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

}
